<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class OptionViewForm extends MiniyunViewForm
{

    
    public function save($attributes)
    {
        foreach($attributes as $key=>$value){
            $this->setOptionValue($key, $value);
        }
        return true;
    }

    
    public function getData(){
        $data = array();

        foreach($this->attributeLabels() as $key=>$value){
            $keyUpper = $this->covertNameToLower($key);

            $option = new Option();
            $option_data = $option->find("option_name='{$keyUpper}'", array("'{$keyUpper}'"=>$keyUpper));
            if (empty($option_data)){
                $option_value = "";
            } else {
                $option_value = $option_data->option_value;
            }

                        $value_detail = array();
            $value_detail[0] = $value;
            $value_detail[1] = $option_value;

            $data[$key] = $value_detail;
        }
        return $data;
    }

    
    public function setOptionValue($key, $value){
        $key = $this->covertNameToLower($key);
        $option_data = Option::model()->find("option_name='{$key}'", array("'{$key}'"=>$key));

        if (empty($value) || $value == ""){
            $value = "";
        }

        if (!empty($option_data)){
            if ($option_data->option_value != $value){
                $option_data->option_value = $value;
                $option_data->save();
            }
        } else {
            $option=new Option;
            $option->option_name =$key;
            $option->option_value=$value;
            $option->save();
        }
        return true;
    }

    
    public function getOptionValue($key){
        $keyUpper = $this->covertNameToLower($key);

        $option = new Option();
        $option_data = $option->find("option_name='{$keyUpper}'", array("'{$keyUpper}'"=>$keyUpper));

        if (empty($option_data)){
            $option_value = "";
        } else {
            $option_value = $option_data->option_value;
        }
        return $option_value;
    }
}